import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/department_regional_organization_tree/regional_organization_tree_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/common/department_user_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/hidden_danger_investigation_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/danger_investigation_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

///隐患排查 排查计划台账
class InvestigationPlanLedgerController extends GetxController {
  /// 选中的节点
  DepartmentUserTreeEntity selectedEntity = DepartmentUserTreeEntity();

  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();
  String searchX = '';

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  /// 筛选按钮
  void onFilterButtonPressed() {
    final DangerInvestigationController controller = Get.find<DangerInvestigationController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    // riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  ///排查计划台账点击详情
  void onClickButtonPressed(HiddenDangerInvestigationLedgerRecords item) {
    Get.toNamed(AppRoutes.investigationPlanLedgerDetail, arguments: item);
  }

  Future<List<HiddenDangerInvestigationLedgerRecords>> getInstrumentData(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<HiddenDangerInvestigationLedgerRecords>>(
      Interface.hiddenDangerInvestigationLedger,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'planName': searchX,
        'departmentId': selectedEntity.id, //部门id
      },
    ).execute();
    List<HiddenDangerInvestigationLedgerRecords>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    return list!;
  }

  /// 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  /// 重置
  void reset() {
    Get.find<DangerInvestigationController>().riskRightScaffoldKey.currentState?.closeEndDrawer();
    selectedEntity = DepartmentUserTreeEntity();
    globalKey.currentState!.onRefresh();
    update();
  }

  /// 确定
  void confirm() {
    Get.back();
    globalKey.currentState!.onRefresh();
    update();
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: Column(
        children: [
          DepartmentRegionalOrganizationTreePage(
            selectedEntity: selectedEntity,
            onCallBack: (DepartmentUserTreeEntity entity) {
              selectedEntity = entity;
            },
          ),
        ],
      ),
      reset: reset,
      confirm: confirm,
    );
  }
}
